import random

def generate_id_number():
    # 生成随机的地址码（6位）
    def generate_address_code():
        return ''.join(str(random.randint(1, 9)) for _ in range(6))

    # 生成出生日期码（8位，格式为YYYYMMDD）
    def generate_birth_date():
        year = str(random.randint(1958, 2007))
        month = '{:02d}'.format(random.randint(1, 12))
        day = '{:02d}'.format(random.randint(1, 28))
        return year + month + day

    # 生成顺序码（3位，奇数分配给男性，偶数分配给女性）
    def generate_order_code(gender):
        base = random.randint(0, 498)  # 范围000到998，确保不会超过999
        if gender == 'male':
            return '{:03d}'.format(base * 2 + 1)  # 男性为奇数
        else:
            return '{:03d}'.format(base * 2)       # 女性为偶数

    # 生成校验码（1位）
    def generate_check_digit(id_number):
        weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        check_sum = sum(int(id_number[i]) * weights[i] for i in range(17)) % 11
        check_digit_map = {0: '1', 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2'}
        return check_digit_map[check_sum]

    ids = []
    for _ in range(5):
        address = generate_address_code()
        birth = generate_birth_date()
        gender = 'male' if random.choice([True, False]) else 'female'
        order = generate_order_code(gender)
        id_number = address + birth + order
        # 在添加校验码之前，先检查生成的ID号码长度
        if len(id_number) != 17:
            raise ValueError("生成的ID号码长度错误，应为17位。")
        check_digit = generate_check_digit(id_number)
        id_number += check_digit
        ids.append(id_number)
    return ids
